package com.far.game.xiuzhen.service.RedisService;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.far.game.xiuzhen.common.conf.MyBatisFillHandler;
import com.far.game.xiuzhen.common.database.entity.BaseEntity;
import com.far.game.xiuzhen.common.database.entity.SkillBufs;
import com.far.game.xiuzhen.common.database.entity.Skills;
import com.far.game.xiuzhen.common.utils.RedisUtil;
import com.far.game.xiuzhen.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class RedisCacheData {

    @Autowired
    @Qualifier("skillBufsServiceImpl")
    private ISkillBufsService iSkillBufsService;

    @Autowired
    @Qualifier("skillsServiceImpl")
    private ISkillsService iSkillsService;

    @Autowired
    @Qualifier("serversServiceImpl")
    private IServersService iServersService;


    @Autowired
    @Qualifier("partnerServiceImpl")
    private IPartnerService iPartnerService;

    @Autowired
    @Qualifier("partnerSkillServiceImpl")
    private IPartnerSkillService iPartnerSkillService;

    @Autowired
    @Qualifier("userPartnerServiceImpl")
    private IUserPartnerService iUserPartnerService;


    @Autowired
    RedisUtil redisUtil;

    public void cacheData() {
        this.redisData(iSkillsService);
        this.redisData(iSkillBufsService);
        this.redisData(iServersService);
        this.redisData(iPartnerService);
        this.redisData(iPartnerSkillService);
        this.redisData(iUserPartnerService);

    }


    public <T extends BaseEntity> void redisData(IService<T> data) {
        List<T> dataList = data.list();
        if (ObjectUtil.isNotEmpty(dataList)) {
            dataList.stream().map(e -> {
                String[] clsNames = data.getEntityClass().getName().split("\\.");
                String str = clsNames[clsNames.length - 1] + ":" + e.getTid();
                redisUtil.set(str, e);
                return null;
            }).collect(Collectors.toList());
        }
    }
}
